﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PulseDeviceTest
{
    [StructLayout(LayoutKind.Sequential)]
    public struct WaveParamStc
    {
        //public int BaseLine;       //动态的基准
        //public int SBaseLine;      //静态的基准
        //public int DataBit;        //数据的位数
        //public float VRange;       //电压的范围
        //public float SamplingFrq; //采样频率
        //public float DAmpRatio;   //动态的放大倍数
        //public float SAmpRatio;   //静态的放大倍数
        //public float ConverRatio; //毫伏转克的系数

        //int BaseLine;       //动态的基准
        public int SBaseLine;      //静态的基准
        //int DataBit;        //数据的位数
        //float VRange;       //电压的范围
        public float SamplingFrq; //采样频率
        //float DAmpRatio;   //动态的放大倍数
        //float SAmpRatio;   //静态的放大倍数
        //float ConverRatio; //毫伏转克的系数
        public float mmHg_to_g;
        public float g_to_mmHg;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct WaveAnalyseRlt
    {
        public float h1;
        public float h2;
        public float h3;
        public float h4;
        public float h5;
        public float wqp;
        public float t;
        public float t1;
        public float t2;
        public float t3;
        public float t4;
        public float t5;
        public float w1;
        public float w2;
        public float As;
        public float Ad;
        public float Vh;
        public float Vt;
        public float PAngle;
        public float UAngle;
        public int TWave;
        public int DWave;
        public int PH;
        public float Frq;
        public int FrqType;
        public int Rhythm;
        public int nPower;
        public int nStart;
        public int nBestStart;
        public int nBestEnd;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PulseRstStc
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseName;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulsePos;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseShu;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseXing;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseShi;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseLi;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
        public byte[] PulseJieLv;
    }


    public struct PusleAnalyseRlt
    {
        [MarshalAs(UnmanagedType.Struct)]
        public WaveAnalyseRlt m_WaveRst;

        [MarshalAs(UnmanagedType.Struct)]
        public PulseRstStc m_PulseRst;
    };

    public class PulseResult
    {
        public string PulseName { get; set; }//脉名
        public string PulsePosition { get; set; }//脉位
        public string PulseForce { get; set; }//脉力
        public string PulseRate { get; set; }//脉率
        public string Rhythm { get; set; }//节律
        public string PulsePotential { get; set; }//脉势
        public string PulseH1 { get; set; }//H1
        public List<PulseRstString1> pulseRstString { get; set; }
    }
    public class PulseRstString1
    {
        public string PulseName { get; set; }//脉名
        public string PulsePosition { get; set; }//脉位
        public string PulseForce { get; set; }//脉力
        public string PulseRate { get; set; }//脉率
        public string Rhythm { get; set; }//节律
        public string PulsePotential { get; set; }//脉势
        public string PulseH1 { get; set; }//H1
    }

    public struct PulseRstString
    {        
        public string PulseName;
        
        public string PulsePos;
        
        public string PulseShu;
        
        public string PulseXing;
        
        public string PulseShi;
        
        public string PulseLi;
        
        public string PulseJieLv;
    }

    static class DataConfig
    {
        public static PulseRstString GetPulseRstString(PulseRstStc src)
        {
            PulseRstString res = new PulseRstString();
            byte[] temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseName);
            res.PulseName = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseShu);
            res.PulseShu = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulsePos);
            res.PulsePos = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseXing);
            res.PulseXing = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseShi);
            res.PulseShi = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseLi);
            res.PulseLi = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, src.PulseJieLv);
            res.PulseJieLv = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

            return res;
        }

        //public static WaveParamStc SetupWaveParam()
        //{
        //    WaveParamStc wave_parame = new WaveParamStc();
        //    try
        //    {
        //        wave_parame.BaseLine = int.Parse(ConfigurationManager.AppSettings.Get("BaseLine"));
        //        wave_parame.SBaseLine = int.Parse(ConfigurationManager.AppSettings.Get("SBaseLine"));
        //        wave_parame.DataBit = int.Parse(ConfigurationManager.AppSettings.Get("DataBit"));
                
        //        wave_parame.DAmpRatio = Convert.ToSingle(ConfigurationManager.AppSettings.Get("DAmpRatio"));
        //        wave_parame.SAmpRatio = Convert.ToSingle(ConfigurationManager.AppSettings.Get("SAmpRatio"));
        //        wave_parame.ConverRatio = Convert.ToSingle(ConfigurationManager.AppSettings.Get("ConverRatio"));
        //        wave_parame.VRange = Convert.ToSingle(ConfigurationManager.AppSettings.Get("VRange"));
        //        wave_parame.SamplingFrq = Convert.ToSingle(ConfigurationManager.AppSettings.Get("SamplingFrq"));
        //    }
        //    catch(Exception ex)
        //    {
        //        wave_parame = new WaveParamStc();
        //    }
        //    return wave_parame;
        //}
    }
}
